﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace MopsesPetShop
{
    public partial class Talk : Page
    {
        private DataClasses1DataContext dc;
        protected void ButtonClick(object sender, EventArgs e)
        {
            var date=DateTime.Now;
            if(Request["acc"]==null)
            {
                var b = new Blogs
                            {
                                comment = commentText.Text,
                                date = date,
                                ekInstance = int.Parse(Request["instance"])
                            };
                dc.Blogs.InsertOnSubmit(b);
            }
            else
            {
                var b = new AccBlogs
                {
                    comment = commentText.Text,
                    date = date,
                    ekInstance = int.Parse(Request["instance"])
                };
                dc.AccBlogs.InsertOnSubmit(b);
            }
            dc.SubmitChanges();

            if(Request["acc"]==null)
                CreateBox(false, dc.Blogs.Where(bl => bl.date == date).First().pk);
            else
                CreateBox(true, dc.AccBlogs.Where(bl => bl.date == date).First().pk);
        }

        protected void CreateBox(bool isAcc,int ekBlog)
        {
            var box = new HtmlGenericControl();
            if (!isAcc)
            {
                var blog = dc.Blogs.Where(b => b.pk == ekBlog).First();
                var txt = new HtmlGenericControl
                {
                    InnerText = "Дата: " + blog.date
                };
                txt.Style.Add(HtmlTextWriterStyle.Display, "Block");
                box.Controls.Add(txt);

                txt = new HtmlGenericControl
                {
                    InnerText = "Комментарий: " + blog.comment
                };
                txt.Style.Add(HtmlTextWriterStyle.Display, "Block");
                box.Controls.Add(txt);
            }
            else
            {
                var blog = dc.AccBlogs.Where(b => b.pk == ekBlog).First();
                var txt = new HtmlGenericControl
                {
                    InnerText = "Дата: " + blog.date
                };
                txt.Style.Add(HtmlTextWriterStyle.Display, "Block");
                box.Controls.Add(txt);

                txt = new HtmlGenericControl
                {
                    InnerText = "Комментарий: " + blog.comment
                };
                txt.Style.Add(HtmlTextWriterStyle.Display, "Block");
                box.Controls.Add(txt);
            }
            box.Style.Add(HtmlTextWriterStyle.Display,"block");
            box.Style.Add(HtmlTextWriterStyle.MarginTop, "30px");
            contentMainDiv.Controls.Add(box);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            addCommentButton.Click += ButtonClick;
            dc = new DataClasses1DataContext();
            var ekInstance = int.Parse(Request["instance"]);
            miniTitle.InnerText =
                "Обсуждения для " + Request["kind"] + ": " +
                dc.Instances.Where(ins => ins.pk == int.Parse(Request["instance"])).First().name;
            if(Request["acc"]==null)
                foreach (var blog in dc.Blogs.Where(blog => blog.ekInstance == ekInstance))
                    CreateBox(false, blog.pk);
            else
                foreach (var blog in dc.AccBlogs.Where(blog => blog.ekInstance == ekInstance))
                    CreateBox(true, blog.pk);


            dc.Connection.Close();

        }
    }
}